package cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbFolder;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.genId.PKGenerator;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbFolder.vo.*;
import cn.iocoder.yudao.module.xiaoBizHandler.convert.tbFolder.XTbFolderConvert;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.tbFolder.XTbFolderDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.tbFolder.XTbFolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 文件夹")
@RestController
@RequestMapping("/xiaoBizHandler/xTbFolder")
@Validated
public class XTbFolderController {

    @Resource
    private XTbFolderService xTbFolderService;

    @PostMapping("/create")
    @ApiOperation("创建文件夹")
    @PreAuthorize("@ss.hasPermission('xbh:folder:create')")
    public CommonResult<String> createXTbFolder(@Valid @RequestBody XTbFolderCreateReqVO createReqVO) {
        createReqVO.setId(PKGenerator.newId());
        createReqVO.setStatus(0);
        return success(xTbFolderService.createXTbFolder(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新文件夹")
    @PreAuthorize("@ss.hasPermission('xbh:folder:update')")
    public CommonResult<Boolean> updateXTbFolder(@Valid @RequestBody XTbFolderUpdateReqVO updateReqVO) {
        xTbFolderService.updateXTbFolder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除文件夹")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('xbh:folder:delete')")
    public CommonResult<Boolean> deleteXTbFolder(@RequestParam("id") String id) {
        xTbFolderService.deleteXTbFolder(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得文件夹")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('xbh:folder:query')")
    public CommonResult<XTbFolderRespVO> getXTbFolder(@RequestParam("id") String id) {
        XTbFolderDO xTbFolder = xTbFolderService.getXTbFolder(id);
        return success(XTbFolderConvert.INSTANCE.convert(xTbFolder));
    }

    @GetMapping("/list")
    @ApiOperation("获得文件夹列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('xbh:folder:query')")
    public CommonResult<List<XTbFolderRespVO>> getXTbFolderList(XTbFolderListReqVO  reqVO) {
        List<XTbFolderDO> list = xTbFolderService.getSimpleFolders(reqVO);
        list.sort(Comparator.comparing(XTbFolderDO::getSort));
        return success(XTbFolderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得文件夹分页")
    @PreAuthorize("@ss.hasPermission('xbh:folder:query')")
    public CommonResult<PageResult<XTbFolderRespVO>> getXTbFolderPage(@Valid XTbFolderPageReqVO pageVO) {
        PageResult<XTbFolderDO> pageResult = xTbFolderService.getXTbFolderPage(pageVO);
        return success(XTbFolderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出文件夹 Excel")
    @PreAuthorize("@ss.hasPermission('xbh:folder:export')")
    @OperateLog(type = EXPORT)
    public void exportXTbFolderExcel(@Valid XTbFolderExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<XTbFolderDO> list = xTbFolderService.getXTbFolderList(exportReqVO);
        // 导出 Excel
        List<XTbFolderExcelVO> datas = XTbFolderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "文件夹.xls", "数据", XTbFolderExcelVO.class, datas);
    }



    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取文件夹精简信息列表", notes = "只包含被开启的文件夹，主要用于前端的下拉选项")
    public CommonResult<List<XTbFolderSimpleRespVO>> getSimpleFolders() {
        // 获得部门列表，只要开启状态的
        XTbFolderListReqVO reqVO = new XTbFolderListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<XTbFolderDO> list = xTbFolderService.getSimpleFolders(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(XTbFolderDO::getSort));
        List<XTbFolderSimpleRespVO> data = XTbFolderConvert.INSTANCE.convertList03(list);
        for (int i = 0; i < data.size(); i++) {
            XTbFolderSimpleRespVO datum = data.get(i);
            String name = datum.getName();
            datum.setNameFirstPinYin(StrUtils.toFirstChar(name));
            datum.setNameAllPinYin(StrUtils.toPinyin(name));
        }
        return success(data);
    }


    

}
